﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for DoiMatKhau.xaml
    /// </summary>
    public partial class DoiMatKhau : Window
    {
        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        private string m_MaNguoiDung;
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private XuLyThem m_cXuLyThem = new XuLyThem();
        public DoiMatKhau(string MaDocGia)
        {
            InitializeComponent();
            m_MaNguoiDung = MaDocGia;
        }

        private bool KiemTraNhap()
        {
            if (txtMKcu.Password == "" || txtMKmoi.Password == "")
                return true;
            else return false;
        }

        private void bntChapNhan_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Hãy nhập đầy đủ thông tin", "Thông báo");
            else
            {
                if (m_cXuLyThem.LoaiNguoiDung(m_MaNguoiDung) == "DG")
                {
                    if (serv.SelectDoiMatKhau(m_MaNguoiDung) == 0)
                    {
                        if (txtMKcu.Password == serv.SelectMatKhau(m_MaNguoiDung).ToString())
                            m_updateInfo.UpdateMatKhau(m_MaNguoiDung, txtMKmoi.Password);
                        MainWindow m_fMainWindow = new MainWindow(m_MaNguoiDung);
                        m_fMainWindow.Show();
                        this.Close();
                        m_updateInfo.UpdateDoiMatKhau(m_MaNguoiDung, 1);
                    }
                    else
                    {
                        if (txtMKcu.Password == serv.SelectMatKhau(m_MaNguoiDung).ToString())
                            m_updateInfo.UpdateMatKhau(m_MaNguoiDung, txtMKmoi.Password);
                        else MessageBox.Show("Mật khẩu cũ chưa chính xác", "Thông báo");
                        //MainWindow m_fMainWindow = new MainWindow();
                        this.Close();
                    }
                }
                if (m_cXuLyThem.LoaiNguoiDung(m_MaNguoiDung) == "NV")
                {
                    if (serv.SelectDoiMatKhau_NhanVien(m_MaNguoiDung) == 0)
                    {
                        if (txtMKcu.Password == serv.SelectMatKhau_NhanVien(m_MaNguoiDung).ToString())
                            m_updateInfo.UpdateMatKhau_NhanVien(m_MaNguoiDung, txtMKmoi.Password);
                        MainWindow m_fMainWindow = new MainWindow(m_MaNguoiDung);
                        m_fMainWindow.Show();
                        this.Close();
                        m_updateInfo.UpdateDoiMatKhau_NhanVien(m_MaNguoiDung, 1);
                    }
                    else
                    {
                        if (txtMKcu.Password == serv.SelectMatKhau_NhanVien(m_MaNguoiDung).ToString())
                            m_updateInfo.UpdateMatKhau_NhanVien(m_MaNguoiDung, txtMKmoi.Password);
                        else MessageBox.Show("Mật khẩu cũ chưa chính xác", "Thông báo");
                        //MainWindow m_fMainWindow = new MainWindow();
                        this.Close();
                    }
                }
            }
        }

        private void bntThoat_Click(object sender, RoutedEventArgs e)
        {
            if (serv.SelectDoiMatKhau(m_MaNguoiDung) == 0)
            {
                this.Close();
                DangNhap m_fDangNhap = new DangNhap();
                m_fDangNhap.Show();
            }
            else this.Close();
        }

        private void txtMKcu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_Click(sender, e);
        }

        private void txtMKmoi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_Click(sender, e);
        }
    }
}
